//
//  CHWebAlert.swift
//  bridging_js
//
//  Created by cyptt on 2021/7/30.
//

import UIKit
import WebKit

class CHWebAlert: NSObject {

    
    init(parentController:UIViewController,resultStr:String,webView:WKWebView) {
        super.init()

    
        var alertModelM:CHWebAlertModel?
        var baseModelM:CHWebBaseModel<CHWebAlertModel>?
        
        if let baseModel = CHWebBaseModel<CHWebAlertModel>.deserialize(from: resultStr ){
            
            baseModelM = baseModel
            alertModelM = baseModel.body

        }
        
        let alertController = UIAlertController(title: alertModelM?.title, message: alertModelM?.body, preferredStyle: UIAlertController.Style.alert)
        let cancelAction = UIAlertAction(title: alertModelM?.leftName, style: UIAlertAction.Style.default) { (UIAlertAction) in
            print("取消")
            CHWebCommon.sendJsResultContent(webView: webView, responseObject: "弹出框取消回调结果", callbackActiveName: C_AlertCancel, isSucess: true);
            
        }
        let ok = UIAlertAction(title: alertModelM?.rightName, style: UIAlertAction.Style.default) { (UIAlertAction) in
            print("确定")

            CHWebCommon.sendJsResultContent(webView: webView, responseObject: "弹出框确定回调结果", callbackActiveName: C_AlertSure, isSucess: true);
        }
        alertController.addAction(cancelAction)
        alertController.addAction(ok)
        parentController.present(alertController, animated: true, completion: nil)
    }
}
